package com.cs.framework.ui.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cs.framework.common.util.JSONUtils;
import com.cs.framework.common.util.StringUtils;
import com.cs.framework.core.context.Application;
import com.cs.framework.ui.builder.UIBuilder;
import com.cs.framework.ui.model.PageInfo;
import com.cs.framework.ui.model.UIInfo;
import com.cs.framework.ui.util.UIConstant;
import com.cs.framework.ui.util.UIUtils;
import com.cs.framework.web.util.RequestUtils;
import com.cs.framework.web.util.WebConstant;
import com.cs.framework.web.view.DefaultViewName;
import com.cs.framework.web.view.View;
import com.cs.framework.web.view.ViewData;
import com.cs.framework.web.view.ViewInfo;

/**
 * 页面视图
 * 
 * @author hedongzhou
 * 
 */
public class UIView implements View {

	private final Log logger = LogFactory.getLog(getClass());

	/**
	 * 应用上下文
	 */
	private Application application;

	/**
	 * 公共构建
	 */
	private UIBuilder builder;

	/**
	 * 页面模板名称
	 */
	private String tplName;

	/**
	 * 初始化
	 * 
	 * @param application
	 *            应用上下文
	 */
	public void init(Application application) {
		this.application = application;

		builder = application.getBean(UIBuilder.class);

		tplName = (String) application.getPropertyMap().get(UIConstant.PAGE_TEMPLATE_NAME_PROPERTY);
		if (StringUtils.isBlank(tplName)) {
			tplName = UIConstant.DEFAULT_PAGE_TEMPLATE_NAME;
		}
	}

	/**
	 * 渲染视图
	 * 
	 * @param request
	 * @param response
	 * @param viewInfo
	 * @return
	 */
	public ViewInfo render(HttpServletRequest request, HttpServletResponse response, ViewInfo viewInfo) {
		ViewData viewData = viewInfo.getViewData();

		String superId = (String) viewData.getAttribute(WebConstant.UI_SUPER_ID_KEY);
		if (StringUtils.isBlank(superId)) {
			superId = "";
		}

		// 构建页面
		UIInfo info = builder.build(viewInfo.getViewPath(), superId);

		if (UIUtils.isPage(info)) {
			PageInfo pageInfo = (PageInfo) info;

			viewData.remove(WebConstant.UI_SUPER_ID_KEY);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("title", pageInfo.getTitle());
			map.put("rootUrl", RequestUtils.getRoot(request) + "/");
			map.put("moduleUrl", RequestUtils.getRoot(request) + "/" + pageInfo.getModuleId() + "/");
			map.put("page", JSONUtils.toString(info));
			map.put("event", UIUtils.getPageJs(pageInfo.getPageId(), application.getModule(pageInfo.getModuleId())
					.getPackageUrl()));

			if (!viewData.isEmpty()) {
				map.put("data", JSONUtils.toString(viewData));
			}

			response.setContentType("text/html;charset=UTF-8");

			try {
				UIUtils.exportView(tplName, map, response.getWriter());
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new RuntimeException(e);
			}

			return null;
		} else {
			// 设置数据
			viewData = new ViewData();
			viewData.addAttribute(WebConstant.UI_INFO_ATTR, info);

			// 创建视图信息
			viewInfo = new ViewInfo(DefaultViewName.JSON_VIEW);
			viewInfo.setViewData(viewData);

			return viewInfo;
		}
	}

}
